const http = require('http');
const io = require('socket.io');

let httpServer = http.createServer();
httpServer.listen(8899);

let wsServer = io(httpServer);
let socketLink = [];
wsServer.on('connection', socket => {
  // socket.emit
  // socket.on
  socketLink.push(socket);
  socket.on('send', str => {
    socketLink.forEach(so => {
      if (so != socket) {
        so.emit('send', str);
      }
    });
  });

  // 断开连接
  socket.on('disconnect', () => {
    let idx = socketLink.indexOf(socket);
    if (idx != -1) {
      socketLink.splice(idx, 1);
    }
  });
});
